package com.koron.sinoprobe.web.purchase.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.business.workflow.templates.WorkFlowHandler;
import com.koron.common.core.interceptor.OrderByInterceptor;
import com.koron.sinoprobe.config.Constants;
import com.koron.sinoprobe.web.purchase.bean.po.SpPurchaseAccept;
import com.koron.sinoprobe.web.purchase.bean.po.SpPurchaseGoods;
import com.koron.sinoprobe.web.purchase.bean.po.SpPurchaseStaffs;
import com.koron.sinoprobe.web.purchase.bean.query.SpPurchaseAcceptQuery;
import com.koron.sinoprobe.web.purchase.bean.vo.SpPurchaseAcceptVO;
import com.koron.sinoprobe.web.purchase.mapper.SpPurchaseAcceptMapper;
import com.koron.sinoprobe.web.purchase.service.SpPurchaseAcceptService;
import com.koron.sinoprobe.web.purchase.service.SpPurchaseGoodsService;
import com.koron.sinoprobe.web.purchase.service.SpPurchaseStaffsService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 采购验收(SpPurchaseAccept)表服务实现类
 *
 * @author jzp
 * @since 2021-12-29 10:19:38
 */
@Service
public class SpPurchaseAcceptServiceImpl implements SpPurchaseAcceptService {

    @Autowired
    private SpPurchaseGoodsService spPurchaseGoodsService;

    @Autowired
    private SpPurchaseStaffsService spPurchaseStaffsService;

    @Autowired
    private WorkFlowHandler workFlowHandler;

    /**
     * 通过ID查询单条数据
     */
    @Override
    @TaskAnnotation("getById")
    public SpPurchaseAcceptVO getById(SessionFactory factory, String id) {
        SpPurchaseAcceptMapper mapper = factory.getMapper(SpPurchaseAcceptMapper.class);
        SpPurchaseAcceptVO spPurchaseAcceptVO = mapper.selectById(id);

        // 物品清单
        List<SpPurchaseGoods> spPurchaseGoodsList = spPurchaseGoodsService.listByAcceptId(factory, id);
        spPurchaseAcceptVO.setSpPurchaseGoodsList(spPurchaseGoodsList);

        // 验收人员
        List<SpPurchaseStaffs> spPurchaseStaffs = spPurchaseStaffsService.listByAcceptId(factory, id);
        spPurchaseAcceptVO.setSpPurchaseStaffs(spPurchaseStaffs);

        return spPurchaseAcceptVO;
    }

    /**
     * 查询所有数据
     */
    @Override
    @TaskAnnotation("list")
    public List<SpPurchaseAcceptVO> list(SessionFactory factory, SpPurchaseAcceptQuery query) {
        SpPurchaseAcceptMapper mapper = factory.getMapper(SpPurchaseAcceptMapper.class);
        return mapper.selectList(query);
    }

    @Override
    @TaskAnnotation("page")
    public PageInfo<SpPurchaseAcceptVO> page(SessionFactory factory, PageQuery pageQuery, SpPurchaseAcceptQuery query) {
        OrderByInterceptor.setOrderBy(pageQuery.getOrders());            // 设置排序，若不设置排序，插件会因为没有排序字段而直接跳过，不会影响分页插件
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize());  //设置分页
        List<SpPurchaseAcceptVO> spPurchaseAcceptList = list(factory, query);
        return new PageInfo<>(spPurchaseAcceptList);
    }

    /**
     * 新增数据
     */
    @Override
    @TaskAnnotation("saveOrUpdate")
    public boolean saveOrUpdate(SessionFactory factory, SpPurchaseAccept spPurchaseAccept) {
        // 默认草稿
        if (StringUtils.isEmpty(spPurchaseAccept.getStatus())) {
            spPurchaseAccept.setStatus(Constants.AppceptStatus.DRAFT);
        }
        SpPurchaseAcceptMapper mapper = factory.getMapper(SpPurchaseAcceptMapper.class);
        return mapper.insertOrUpdate(spPurchaseAccept) > 0;
    }

    @Override
    @TaskAnnotation("saveOrUpdateVo")
    public boolean saveOrUpdateVo(SessionFactory factory, SpPurchaseAcceptVO spPurchaseAcceptVO) {
        // 1. 保存主题信息
        this.saveOrUpdate(factory, spPurchaseAcceptVO);

        // 2. 保存物品清单
        spPurchaseGoodsService.removeByAcceptId(factory, spPurchaseAcceptVO.getId());
        spPurchaseAcceptVO.getSpPurchaseGoodsList().forEach(e -> e.setAcceptId(spPurchaseAcceptVO.getId()));
        spPurchaseGoodsService.saveBatch(factory, spPurchaseAcceptVO.getSpPurchaseGoodsList());

        // 3. 保存验收人员
        spPurchaseStaffsService.removeByAcceptId(factory, spPurchaseAcceptVO.getId());
        spPurchaseAcceptVO.getSpPurchaseStaffs().forEach(e -> e.setAcceptId(spPurchaseAcceptVO.getId()));
        spPurchaseStaffsService.saveBatch(factory, spPurchaseAcceptVO.getSpPurchaseStaffs());

        return true;
    }

    @Override
    @TaskAnnotation("end")
    public boolean end(SessionFactory factory, String id) {
        SpPurchaseAccept spPurchaseAccept = new SpPurchaseAccept();
        spPurchaseAccept.setId(id);
        spPurchaseAccept.setStatus(Constants.AppceptStatus.APPROVED);
        return update(factory, spPurchaseAccept);
    }

    @Override
    @TaskAnnotation("submit")
    public boolean submit(SessionFactory factory, SpPurchaseAcceptVO spPurchaseAcceptVO) {
        spPurchaseAcceptVO.setStatus(Constants.AppceptStatus.WAIT_REVIEW);
        saveOrUpdateVo(factory, spPurchaseAcceptVO);
        return true;
    }

    /**
     * 修改数据
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, SpPurchaseAccept spPurchaseAccept) {
        SpPurchaseAcceptMapper mapper = factory.getMapper(SpPurchaseAcceptMapper.class);
        return mapper.update(spPurchaseAccept) > 0;
    }

    @Override
    @TaskAnnotation("updateStatus")
    public boolean updateStatus(SessionFactory factory, String id, String status) {
        SpPurchaseAccept spPurchaseAccept = new SpPurchaseAccept();
        spPurchaseAccept.setId(id);
        spPurchaseAccept.setStatus(status);
        return update(factory, spPurchaseAccept);
    }

    /**
     * 通过主键删除数据
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        SpPurchaseAccept source = getById(factory, id);
        Assert.state(source.getStatus().equals(Constants.AppceptStatus.DRAFT), "非草稿状态不允许删除");
        SpPurchaseAcceptMapper mapper = factory.getMapper(SpPurchaseAcceptMapper.class);
        return mapper.hidden(id) > 0;
    }
}
